/*
 *      comm.c
 *      
 *      Copyright 2009 Daniele Ricci <denn86@gmail.com>, Luca Saccagi <luksak86@gmail.com>
 *      
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */

#ifndef _COMM_IMPL
#define _COMM_IMPL

#include "comm.h"
/**\brief Gestisce la ricezione di un frame BEEP
 *
 * Questa funzione gestisce la ricezione e l'elaborazione di un frame BEEP contenente una query a idmp.
 * \param VortexChannell *ch canale da cui ricevere i frame
 * \param VortexConnection *cn connessione da cui ricevere i frame
 * \param VortexFrame *frame frame ricevuto
 *
 * Dopo aver ricevuto un frame e aver controllato lo stato di autenticazione si passa al processing della query
 * contenuta nel frame richiamando queryexec e passando come query il payload del frame.
 *
 */
#ifdef _WIN32 
static void frame_received (VortexChannel *ch,VortexConnection * cn,VortexFrame *frame,axlPointer user_data){
#else
void frame_received (VortexChannel *ch,VortexConnection * cn,VortexFrame *frame,axlPointer user_data){
#endif
	if(!vortex_sasl_is_authenticated(cn)){
		vortex_channel_close(ch,NULL);
		vortex_connection_close(cn);
	}
	msg_no=vortex_frame_get_msgno(frame);
	vortex_mutex_lock(&mutex);
	/*DBG*/DEBUG == DBG_MUTEX ? (printf("DBG - (locked) FRAME_RECEIVED\n"),getchar()) : 0;
	queryexec(cn,ch,(char *)vortex_frame_get_payload(frame));
	/*DBG*/DEBUG == DBG_MUTEX ? (printf("DBG - (unlocking) FRAME_RECEIVED\n"),getchar()) : 0;
	vortex_mutex_unlock(&mutex);
	/*DBG*/DEBUG == DBG_MUTEX ? printf("DBG - (unlocked) FRAME_RECEIVED\n") : 0;
	return;
}

/**\brief Esegue la validazione dell'autenticazione SASL
 *
 */
#ifdef _WIN32 
static VortexSaslAuthCramMd5 sasl_cram_md5_validation(VortexConnection *connection,const char *auth_id,const char *authorization_id,const char *password){
#else
VortexSaslAuthCramMd5 sasl_cram_md5_validation(VortexConnection *connection,const char *auth_id,const char *authorization_id,const char *password){
#endif
	char *pw;
	
	pw=(char *)calloc(sizeof(char),7);
	strncpy(pw,c0->pw,strlen(c0->pw));
	if(axl_cmp(auth_id,c0->usr)){
		return (VortexSaslAuthCramMd5)pw;
	}else printf("ERR - User %s NOT AUTHENTICATED\n",auth_id);
	return NULL;
}
/**\brief Recupera il certificato x509
 *
 * Recupera il certificato in formato x509 richiamando la funzione vortex_support_find_data_file alla quale passa come
 * parametro il nome del file che lo contiene
 * \param VortexConnection *connection connessione su cui effettuare l'autenticazione
 * \param char *serverName nome del server
 * \return Restituisce il certificato come array di caratteri
 */
#ifdef _WIN32 
static char *certificate_file_location(VortexConnection *connection,char *serverName){
#else
char *certificate_file_location(VortexConnection *connection,char *serverName){
#endif
	return vortex_support_find_data_file(c0->certificate);
}
/**\brief Recupera la chiave privata
 *
 * Recupera la chiave privata richiamando la funzione vortex_support_find_data_file alla quale passa come
 * parametro il nome del file che la contiene
 * \param VortexConnection *connection connessione su cui effettuare l'autenticazione
 * \param char *serverName nome del server
 * \return Restituisce la chiave privata come array di caratteri
 */
#ifdef _WIN32 
static char *private_key_file_location(VortexConnection *connection,char *serverName){
#else
char *private_key_file_location(VortexConnection *connection,char *serverName){
#endif
	return vortex_support_find_data_file(c0->certificate_key); 
}

#endif
